package es.usj;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import es.usj.model.Sintoma;

public class SelectorSintomasSeleccionadosListViewAdapter extends BaseAdapter {
	//nota si cambiamos algo llamar despues a notifyDataSetChanged();
	Context mContext;
	LayoutInflater inflater;
	private List<Sintoma> sintomaslist = null;
	private ArrayList<Sintoma> arraylist;
	
	GestorDatosAlmacenados gda = null;
	
	public SelectorSintomasSeleccionadosListViewAdapter(Context context, List<Sintoma> sintomas) {
		mContext = context;
		this.sintomaslist = sintomas;
		inflater = LayoutInflater.from(mContext);
		this.arraylist = new ArrayList<Sintoma>();
		this.arraylist.addAll(sintomaslist);
	}

	public class ViewHolder {
		TextView codigoSintoma;
		TextView descripcion;
	}

	@Override
	public int getCount() {
		return sintomaslist.size();
	}

	@Override
	public Sintoma getItem(int position) {
		return sintomaslist.get(position);
	}

	@Override
    public long getItemId(int position) {
      //String item = getItem(position);
      return position; //mIdMap.get(item);
    }

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.selectorsintomaslistview_item, null/*parent*/);
			// Locate the TextViews in listview_item.xml
			holder.codigoSintoma = (TextView) view.findViewById(R.id.codigo);
			holder.descripcion = (TextView) view.findViewById(R.id.descripcion);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		// Set the results into TextViews
		holder.codigoSintoma.setText(String.valueOf(sintomaslist.get(position).getCodigoSintoma()));
		holder.descripcion.setText(sintomaslist.get(position).getDescripcion());

/*		(ListView) view.setOnLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
                Log.v("long clicked","pos: " + pos + " id:" + id);
            	if (gda == null) {
            		gda = new GestorDatosAlmacenados(getBaseContext());
            	}
            	try {
					sintomas = gda.getSintomas(getApplicationContext());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                Sintoma sintomaSeleccionado = sintomas.get(pos);
                
                if (sintomaSeleccionado != null) {
                	//comprobamos si ya estaba seleccionado
                	boolean flagBorrar = false;
                	for (Sintoma sintomaTmp : sintomasParaEnviar) {
                		if (sintomaTmp.getCodigoSintoma() == sintomaSeleccionado.getCodigoSintoma()
                				&& sintomaTmp.getSubtipo() == sintomaSeleccionado.getSubtipo()) {
                			flagBorrar = true;
                		}
                	}
                	if (flagBorrar) {
                		//borramos
                        sintomasParaEnviar.remove(sintomaSeleccionado);
                	} else {
                        sintomasParaEnviar.add(sintomaSeleccionado);
                	}
                }

                return true;
            }
        }); */

		
		/*
		// Listen for ListView Item Click
		view.setOnClickListener(new OnClickListener() {
			 
			@Override
			public void onClick(View arg0) {
				//remove items from list
				Intent intent = new Intent(mContext, SelectorSintomasSingleItemView.class);
				intent.putExtra("codigoSintoma",(sintomaslist.get(position).getCodigoSintoma()));
				intent.putExtra("descripcion",(sintomaslist.get(position).getDescripcion()));
				mContext.startActivity(intent);
			}
		});
*/		 
		return view;
	}
}
